//
//  LoginViewModel.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/9/28.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

class LoginViewModel: NSObject {
    
    var pageType:Int = 0
    
    
    override init() {
        super.init()
        
        NotificationCenter.default.addObserver(self, selector: #selector(reloadFinished(notif:)), name: NSNotification.Name("reloadTripFinished"), object: nil)
    }
    
    deinit {
        
        NotificationCenter.default.removeObserver(self)
    }
    
    @objc private func reloadFinished(notif:Notification) {
        
        if let errmsg = notif.userInfo?["errmsg"] as? String {
            
            self.toastText?(errmsg)
            self.loginSuccess?(false,false)
            return
        }
        
        if self.pageType == 1 {
            
            self.loginSuccess?(true,false)
        }
        
        
        if TripManager.share.trips?.count ?? 0 > 0 {
            
            self.loginSuccess?(true,false)
        } else {
            
            self.loginSuccess?(true,true)
        }
    }
    
    var country:Country? {
        
        didSet {
            
            self.reloadCountry?()
        }
    }
    
    var toastText:((String?)-> Void)?
    var loginSuccess:((_ success:Bool,_ needScan:Bool)->Void)?
    var getCodeSuccess:((_ success:Bool)->Void)?
    var reloadCountry:(()->Void)?
    
    var mobile:String?
    var code:String?
    
    func getCode(phone:String?) {
        
        guard let country = country else {
            
            self.toastText?(LocalizedString("choose_country_tips"))
            self.getCodeSuccess?(false)
            return
        }
        
        guard let phone = phone,phone.count > 0 else {
            
            self.toastText?(LocalizedString("input_mobile_tips"))
            self.getCodeSuccess?(false)
            return
        }
        
        NetAPI.encode(country: country.code!, cellphone: phone
            , useage:.quicklogin).request {
                [weak self] response, errmsg in
                guard let `self` = self else { return }
                
                self.toastText?(errmsg ?? LocalizedString("success"))
                
                if let _ = errmsg {
                    
                    self.getCodeSuccess?(false)
                } else {
                    
                    self.getCodeSuccess?(true)
                }
        }
    }
    
    
    func login(phone:String?,code:String?) {
        
        guard let country = country else {
            
            self.toastText?(LocalizedString("choose_country_tips"))
            self.loginSuccess?(false,false)
            return
        }
        
        guard let phone = phone,phone.count > 0 else {
            
            self.toastText?(LocalizedString("input_mobile_tips"))
            self.loginSuccess?(false,false)
            return
        }
        
        guard let code = code,code.count > 0 else {
            
            self.toastText?(LocalizedString("input_code_tips"))
            self.loginSuccess?(false,false)
            return
        }
        
        
        if (UserManager.share.token?.count ?? 0 > 0) && (phone == self.mobile) && (code == self.code)  {
            
            self.checkTrips()
            return
        }
        
        
        NetAPI.login(country: country.code!, cellphone: phone, encode: code).request {
            
            [weak self] response, errmsg in
            guard let `self` = self else { return }
            
            if let errmsg = errmsg {
                
                self.toastText?(errmsg)
                self.loginSuccess?(false,false)
                
                return
            }
            
            if let res = response as? [String:Any], let token = res["token"] as? String {
                
                UserManager.share.token = token
            }
            
            if self.pageType == 1 {
                
                self.loginSuccess?(true,true)
                
                return
            }
            
            self.checkTrips()
        }
        
    }
    
    func checkTrips() {
        
        TripManager.share.reloadTripData()
    }
    
    func bindTrip(tripid:String) {
        
        TripManager.share.bindTrip(tripid: tripid) {
            
            [weak self] errmsg in
            guard let `self` = self else { return }
            
            
            if let errmsg = errmsg {
                
                self.toastText?(errmsg)
                self.loginSuccess?(false,false)
                return
            }
            
            self.checkTrips()
        }
    }
    
}
